package com.pm.controller.awardPunishment;


import com.github.pagehelper.PageInfo;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.awardPunishment.AwardPunishment;
import com.pm.entity.po.security.User;
import com.pm.service.awardPunishment.AwardPunishmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Api(tags = "加减分项")
@UserLoginToken
@RestController
@RequestMapping("/AwardPunishment")
public class AwardPunishmentController {
    @Autowired
    AwardPunishmentService awardPunishmentService;

    /**
     * 查询全部账号
     * @return
     */
    @ApiOperation("查询加减分项")
    @PostMapping("/getAllAward")
    @ResponseBody
    public Response<PageInfo<AwardPunishment>> getAllAward(@RequestBody AwardPunishment awardPunishment) {
        PageInfo<AwardPunishment> result = awardPunishmentService.getAllAward(awardPunishment);
        Response<PageInfo<AwardPunishment>> res = new Response<PageInfo<AwardPunishment>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("添加加减分项")
    @PostMapping("/addAward")
    @ResponseBody
    @MyLog(value = "添加加减分项")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> addAward(@RequestBody AwardPunishment awardPunishment, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = awardPunishmentService.addAward(awardPunishment, user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("修改加减分项")
    @PostMapping("/updateAward")
    @ResponseBody
    @MyLog(value = "修改加减分项")
    public Response updateAward(@RequestBody AwardPunishment awardPunishment, @CurrentUser User user) {
        boolean res = awardPunishmentService.updateAward(awardPunishment, user);
        if (res) {
            return new Response<>(ExpCodeState.update_data_success);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }

    /**
     * 删除
     */
    @ApiOperation("删除加减分")
    @PostMapping("/deleteAward")
    @ResponseBody
    @MyLog(value = "删除加减分")
    public Response<Integer> deleteAward(@RequestParam("id") Integer id) {
        boolean res = awardPunishmentService.deleteAward(id);
        if (res) {
            return new Response<>(ExpCodeState.remove_data_success);
        } else {
            return new Response<>(ExpCodeState.remove_data_failed);
        }
    }

    /**
     * 根据id查部门
     * @return
     */
    @ApiOperation("Id查询负加减分")
    @PostMapping("/getAwardId")
    @ResponseBody
    public Response<AwardPunishment> getAwardId(Integer id) {
        AwardPunishment awardPunishment = awardPunishmentService.getAwardId(id);
        Response<AwardPunishment> response = new Response<>(ExpCodeState.query_data_success, awardPunishment);
        return response;
    }

}
